/*
 * Copyright (C) 2009 codemobiles.com.
 * http://www.codemobiles.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * author: Chaiyasit Tayabovorn 
 * email: chaiyasit.t@gmail.com
 */

package com.codemobiles.droidslator_tattoo.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Handler;
import android.text.ClipboardManager;

import com.codemobiles.droidslator_tattoo.Droidslator;
import com.codemobiles.droidslator_tattoo.R;

public class MyUtil {

  public static Hashtable<String, String> getThaiLettersHashTbl() {
    CharSequence[] strings;

    Resources res = Droidslator.getM_context().getResources();
    strings = res.getTextArray(R.array.thai_letters);
    Hashtable<String, String> thaiLetters = new Hashtable<String, String>();

    for (int i = 0; i < strings.length; i++) {
      thaiLetters.put(strings[i].toString(), String.valueOf(i + 1));
    }
    return thaiLetters;
  }

  public static void clipboard(final String input, Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(input);
  }

  public static void callWebBrowser(final String uriStr, Context context) {
    Intent i = new Intent("android.intent.action.VIEW", Uri.parse(uriStr));
    context.startActivity(i);
  }

  public static final boolean isFileExist(String file) {

    // default partition is /sdcard
    return new File(file).exists();
  }

  public long getZipSize(String zipname) {
    ZipFile zipFile;
    long ret = 0;

    try {
      zipFile = new ZipFile(zipname);

      Enumeration<?> entries = zipFile.entries();
      while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();

        ret += entry.getSize();
      }

      zipFile.close();
    } catch (IOException ioe) {
      return 0;
    }
    return ret;
  }

  public static final boolean copyInputStream(InputStream in, long fileSize, File outFile, Handler mHandler, ProgressGUIUpdate mPgGUIUpdate,
      int mProgressID) throws IOException {
    byte[] buffer = new byte[1024];
    int len;
    float percentProg = 0;
    float acutalFinishedSize = 0;
    float FULL_SCALE = 100;
    DataOutputStream out = null;

    try {

      out = new DataOutputStream(new FileOutputStream(outFile));

      while ((len = in.read(buffer)) >= 0) {
        out.write(buffer, 0, len);
        acutalFinishedSize += len;
        percentProg = (acutalFinishedSize * FULL_SCALE) / fileSize;

        mPgGUIUpdate.updateProgressGUI(mProgressID, percentProg, acutalFinishedSize);

      }

      in.close();
      out.close();
    } catch (Exception ex) {
      return false;
    }
    return true;
  }

  
  public static void alertbox(String title, String mymessage, Context mContext) {
    new AlertDialog.Builder(mContext).setMessage(mymessage).setTitle(title).setCancelable(true).setNeutralButton(android.R.string.cancel,
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
          }
        }).show();
  }
}
